package com.example.zuoye;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class RecordActivity extends AppCompatActivity {

    TextView recordtest1,recordtest2,recordtest3,recordtest4,recordtest5,recordtest6;
    TextView recordtime1,recordtime2,recordtime3,recordtime4,recordtime5,recordtime6;

    public int RECORD_CODE=1010;

    private Handler mhanlder=new Handler(Looper.myLooper())
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            ResultSet rs1 = (ResultSet) msg.obj;
            List<Record> list = new ArrayList<Record>();
            try {
                while (rs1 != null && rs1.next()) {
                    Record r = new Record();
                    r.setCount(rs1.getString("count"));
                    r.setTime(rs1.getString("time"));
                    list.add(r);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            int count=list.size()-1;
            Record r1=(Record)list.get(count-5);
            Record r2=(Record)list.get(count-4);
            Record r3=(Record)list.get(count-3);
            Record r4=(Record)list.get(count-2);
            Record r5=(Record)list.get(count-1);
            Record r6=(Record)list.get(count);

           /* Record r3=(Record)list.get(2);
            Record r4=(Record)list.get(3);
            Record r5=(Record)list.get(4);*/
            //questionview.setText(q.getId());
            recordtest1.setText("    您答错"+ r1.getCount() +"题，请继续努力！");
            recordtime1.setText("学习时间："+r1.getTime());

            recordtest2.setText("    您答错"+ r2.getCount() +"题，请继续努力！");
            recordtime2.setText("学习时间："+r2.getTime());

            recordtest3.setText("    您答错"+ r3.getCount() +"题，请继续努力！");
            recordtime3.setText("学习时间："+r3.getTime());

            recordtest4.setText("    您答错"+ r4.getCount() +"题，请继续努力！");
            recordtime4.setText("学习时间："+r4.getTime());

            recordtest5.setText("    您答错"+ r5.getCount() +"题，请继续努力！");
            recordtime5.setText("学习时间："+r5.getTime());

            recordtest6.setText("    您答错"+ r6.getCount() +"题，请继续努力！");
            recordtime6.setText("学习时间："+r6.getTime());

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);

        recordtest1=(TextView)findViewById(R.id.recordtest1);
        recordtime1=(TextView)findViewById(R.id.recordtime1);

        recordtest2=(TextView)findViewById(R.id.recordtest2);
        recordtime2=(TextView)findViewById(R.id.recordtime2);

        recordtest3=(TextView)findViewById(R.id.recordtest3);
        recordtime3=(TextView)findViewById(R.id.recordtime3);

        recordtest4=(TextView)findViewById(R.id.recordtest4);
        recordtime4=(TextView)findViewById(R.id.recordtime4);

        recordtest5=(TextView)findViewById(R.id.recordtest5);
        recordtime5=(TextView)findViewById(R.id.recordtime5);

        recordtest6=(TextView)findViewById(R.id.recordtest6);
        recordtime6=(TextView)findViewById(R.id.recordtime6);


       /* Intent intent=getIntent();
       final  String r=intent.getStringExtra("record");
       final String t=intent.getStringExtra("time");*/

        Intent intent=getIntent();
        final  String r=intent.getStringExtra("record");
        final String t=intent.getStringExtra("time");


        new Thread(new Runnable() {   //获取sql数据库是耗时操作，只能在线程中进行操作。
            @Override
            public void run() {

                final Connection conn=Connection_Init();
                insertRecord(conn,r,t);

                ResultSet rs=getRecord(conn);
                Message message=new Message();//消息对象
                message.what=0; //区分谁发的消息
                message.obj=rs;
                mhanlder.sendMessage(message);
            }
        }).start();
    }

    public Connection Connection_Init()
    {
        Connection connection = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");//加载驱动
            long in1 = System.currentTimeMillis();
            try {
                connection = DriverManager.getConnection(
                        "jdbc:mysql://10.21.151.218:3306/test?useUnicode=true&characterEncoding=utf8",
                        "root",
                        "root");// 输入连接参数
                System.out.println(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public void insertRecord(Connection conn,String r,String t)
    {
        PreparedStatement mPreparedStatement=null;
        try {
            String sql = "insert into wrong values (?,?)";
            mPreparedStatement=conn.prepareStatement(sql);
            mPreparedStatement.setString(1,r);
            mPreparedStatement.setString(2,t);
            mPreparedStatement.execute();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

    }

    public ResultSet getRecord(Connection conn) {

        ResultSet rs = null;
        try {
            String sql = "select * from wrong";
            Statement stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return rs;

    }

}
